<?php
namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class UserController extends AdminController
{
    /**
     * {@inheritdoc}
     */
    protected function title()
    {
        return trans('admin.administrator');
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $userModel = config('admin.database.users_model');

        $grid = new Grid(new $userModel());

        $grid->column('id', 'ID')->sortable();
        $grid->column('username', trans('admin.username'));
        $grid->column('email', '邮箱');
        $grid->column('roles', trans('admin.roles'))->pluck('name')->label();
        $grid->column('created_at', trans('admin.created_at'));
        $grid->column('updated_at', trans('admin.updated_at'));
        $grid->column('status','启用')->bool()->filter([
            0 => '停用',
            1 => '启用',
        ]);

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->quickSearch('username', 'email');
        $grid->disableFilter();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
//    protected function detail($id)
//    {
//        $userModel = config('admin.database.users_model');
//
//        $show = new Show($userModel::findOrFail($id));
//
//        $show->field('username', trans('admin.username'));
//        $show->field('email', '邮箱');
//        $show->field('roles', trans('admin.roles'))->as(function ($roles) {
//            return $roles->pluck('name');
//        })->label();
//        $show->field('status', '启用')->using([1 => '是',0 => '否']);
//        $show->field('created_at', trans('admin.created_at'));
//        $show->field('updated_at', trans('admin.updated_at'));
//        $show->panel()->tools(function (Show\Tools $tools){
//            $tools->disableDelete();
//        });
//        return $show;
//    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $userModel = config('admin.database.users_model');
        $roleModel = config('admin.database.roles_model');

        $form = new Form(new $userModel());

        $userTable = config('admin.database.users_table');
        $connection = config('admin.database.connection');

        $form->hidden('id', 'ID');
        $form->text('username', trans('admin.username'))
            ->creationRules(['required', "unique:{$connection}.{$userTable}"])
            ->updateRules(['required', "unique:{$connection}.{$userTable},username,{{id}}"])
            ->help('初始密码为123456');

        $form->email('email', '邮箱')->rules('required');
        $form->hidden('password');

        $form->multipleSelect('roles', trans('admin.roles'))->options($roleModel::all()->pluck('name', 'id'));
        $form->switch('status', '启用')->states(statusOptions())->default('1');
        $form->hidden('name');
        $form->hidden('created_at');
        $form->hidden('updated_at');

        $form->saving(function (Form $form) {
            if($form->isEditing()){
                $form->ignore('password');
            }
            if($form->isCreating()){
                $form->password = bcrypt('123456');
            }
            $form->name = $form->username;
        });
        $form->tools(function (Form\Tools $tools){
            $tools->disableDelete();
            $tools->disableView();
        });
        return $form;
    }
}
